<template>
  <div class="register">
    <div class="header">
      <img
        src="../assets/logo1.png"
        alt=""
        style="margin-right: auto; width: 350px"
      />
      <div
        style="
          margin-left: auto;
          display: flex;
          align-items: flex-end;
          font-size: 1.1em;
        "
      >
        已有账户？
        <el-link type="danger" style="font-size: 1.1em" @click="toLogin"
          >立即登录></el-link
        >
      </div>
    </div>
    <div class="main" style="border: red solid 1px">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="userId">
          <el-input v-model="ruleForm.userId"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="ruleForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model.number="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      <Footer></Footer>
    </div>
  </div>
</template>

<script>
import Footer from "@/components/front/Footer.vue";
import axios from "axios";

export default {
  name: "Register",
  components: {
    Footer,
  },
  data() {
    var checkUserid = (rule, value, callback) => {
      // 判断是否为空
      if (!value) {
        return callback(new Error("账号不能为空"));
      }
      
      // 判断是否为数字
      if (!/^\d+$/.test(value)) {
        return callback(new Error("账号只能包含数字"));
      }
      
      callback();
    };
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("昵称不能为空"));
      } else {
        callback();
      }
    };
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("电话号码不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else if (value.length === 11) {
          callback(new Error("请输入11 位的电话号码"));
        } else {
          callback();
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: "",
        checkPass: "",
        phone: "",
        nickname: "",
        userId: "",
        userImg: "",
      },
      message: "",
      rules: {
        userId: [{ validator: checkUserid, trigger: "blur" }],
        nickname: [{ validator: checkName, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        phone: [{ validator: checkPhone, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.register();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    register() {
      console.log(
        this.ruleForm.userId,
        this.ruleForm.nickname,
        this.ruleForm.password,
        this.ruleForm.phone,
        this.ruleForm.userImg
      );
      axios
        .post(
          "user/register",
          {
            userId: this.ruleForm.userId,
            nickname: this.ruleForm.nickname,
            password: this.ruleForm.password,
            phone: this.ruleForm.phone,
            userImg: this.ruleForm.userImg,
          },
          {
            headers: {
              "Content-Type": "application/json", // 设置请求类型为 JSON
            },
          }
        )
        .then((res) => {
          console.log(res.data);
          if (res.data.code == 200) {
            // 注册成功，跳转登录页面
            this.$message({
              message: "注册成功",
              type: "success",
            });
            setTimeout(() => this.$router.push("/login"), 1000);
          } else if (res.data.code == 202) {
            // 用户名已注册，提示用户
            this.$message({
              message: "用户名已被注册！",
              type: "warning",
            });
          } else {
            // 其他错误处理
            this.$message({
              message: "注册失败，请稍后再试！",
              type: "error",
            });
          }
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toLogin() {
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped>
.register {
  background-color: whitesmoke;
  overflow: hidden;
}
.header {
  display: flex;
  justify-content: space-between;
  margin-left: 10%;
  margin-right: 10%;
  height: 20%;
  margin-bottom: 10px;
  margin-top: 70px;
}
.main {
  background-color: white;
  width: 80%;
  margin: auto;
  height: 450px;
  padding-top: 50px;
}
.el-form {
  margin: auto;
  width: 40%;
}
.footer {
  height: 210px;
  margin-top: 15px;
}
.el-button {
  width: 40%;
}
</style>
